<template>
    <div>
        <div>
            <el-input v-model="job.name" placeholder="添加职称"   prefix-icon="el-icon-plus" size="small" style="width: 300px;"></el-input>
            <el-select v-model="job.titleLevel" placeholder="职称等级" style="margin-left: 10px" size="small">
                <el-option
                        v-for="(item,index) in titleLevels"
                        :key="index"
                        :label="item"
                        :value="item">
                </el-option>
            </el-select>
            <el-button type="primary" size="small" style="margin-left: 10px" @click="addJob()">添加</el-button>
            <el-upload style="display: inline-flex;margin-right: 10px;margin-left: 10px"
                       action="/api/basic/job/importJob"
                       :show-file-list="false"
                       :before-upload="beforeUpload"
                       :on-success="onSuccess"
                       :disabled="importDataDisabled"
                       :on-error="onError">
                <el-button  type="success" :icon="importDataBtnIcon" :disabled="importDataDisabled">
                    {{importDataBtnText}}
                </el-button>
            </el-upload>
            <el-button type="success" icon="el-icon-download" @click="exportJob">
                导出数据
            </el-button>
        </div>
        <div style="margin-top: 10px">
            <el-table
                    :data="jobs"
                    border
                    stripe
                    style="width: 60%"
                    size="small"
                    @selection-change="handleSelectionChange">
                <el-table-column
                        type="selection"
                        width="55">
                </el-table-column>
                <el-table-column
                        label="编号"
                        width="100"
                        type="index"
                        :index="indexMethod">
                </el-table-column>
                <el-table-column
                        prop="name"
                        label="名称"
                        width="120">
                </el-table-column>
                <el-table-column
                        prop="titleLevel"
                        label="职称等级"
                        width="140">
                </el-table-column>
                <el-table-column
                        prop="createDate"
                        label="创建时间">
                </el-table-column>
                <el-table-column
                        prop="enabled"
                        label="是否启用">
                    <template slot-scope="scope">
                        <el-tag type="success" v-if="scope.row.enabled">已启用</el-tag>
                        <el-tag type="danger" v-else>未启用</el-tag>
                    </template>
                </el-table-column>
                <el-table-column
                        label="操作"
                width="250">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                @click="showEditView(scope.$index, scope.row)">编辑</el-button>
                        <el-button
                                size="mini"
                                type="danger"
                                @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <el-dialog
                title="修改职称"
                :visible.sync="dialogVisible"
                width="30%">
            <div>
                <table>
                    <tr class="trStyle">
                        <td>
                            <el-tag>职称名</el-tag>
                        </td>
                        <td>
                            <el-input v-model="updateJob.name" size="small"></el-input>
                        </td>
                    </tr>
                    <tr class="trStyle">
                        <td>
                           <el-tag>职称等级</el-tag>
                        </td>
                        <td>
                            <el-select v-model="updateJob.titleLevel" placeholder="请选择">
                                <el-option
                                        v-for="item in titleLevels"
                                        :key="item"
                                        :label="item"
                                        :value="item">
                                </el-option>
                            </el-select>
                        </td>
                    </tr>
                    <tr class="trStyle">
                        <td>
                            <el-tag>是否启用</el-tag>
                        </td>
                        <td>
                            <el-switch
                                    v-model="updateJob.enabled"
                                    active-text="启用"
                                    inactive-text="禁用">
                            </el-switch>
                        </td>
                    </tr>
                </table>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false" size="small">取 消</el-button>
                <el-button type="primary" @click="doUpdate()" size="small">确 定</el-button>
            </span>
        </el-dialog>
        <el-button type="danger" @click="deleteJobs()" :disabled="this.multipleSelection.length == 0" style="margin-top: 10px">批量删除</el-button>
    </div>
</template>

<script>
    export default {
        name: "JobLevelMana",
        data(){
            return {
                importDataBtnText:'导入数据',
                importDataBtnIcon:'el-icon-upload2',
                importDataDisabled:false,
                job:{
                    name:'',
                    titleLevel:'',
                },
                titleLevels:[
                    "正高级",
                    "副高级",
                    "初级",
                    "中级"
                ],
                jobs:[],
                dialogVisible : false,
                updateJob:{
                    name:'',
                    titleLevel:'',
                    enabled : ''
                },
                multipleSelection: []
            }
        },
        mounted (){
            this.initJob();
            //this.initJobTitleLevel();
        },
        methods:{
            //多选框改变时
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            //初始化下拉框的职
            initJobTitleLevel(){
                this.getRequest('/api/system/basic/job/getJobLevelAllTitleLevel').then(resp=>{
                    //console.log(resp);
                    if(resp){
                        this.titleLevels = resp;
                    }
                })
            },
            //初始化表格的值
            initJob(){
                this.getRequest('/api/system/basic/job/').then(resp=>{
                    console.log(resp);
                    if(resp){
                        this.jobs = resp;
                    }
                })
            },
            indexMethod(index) {
                return index + 1;
            },
            //添加职称
            addJob(){
                if(this.job.name && this.job.titleLevel){
                    this.postRequest('/api/system/basic/job/',this.job).then(resp=>{
                        if(resp){
                            this.initJob();
                            this.job.name = '',
                                this.job.titleLevel = ''
                        }
                    })

                }else {
                    this.$message.error('必须填写职称和职称等级');
                }
            },
            //删除
            handleDelete(index,data){
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'error'
                }).then(() => {
                    this.deleteRequest('/api/system/basic/job/' + data.id).then(resp=>{
                        if(resp){
                            this.initJob();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //批量删除
            deleteJobs(){
                    this.$confirm('此操作将永久删除'+ this.multipleSelection.length+'条记录, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'error'
                    }).then(() => {
                        var ids = this.multipleSelection.map(item => item.id).join()//获取所有选中行的id组成的字符串，以逗号分隔
                        this.deleteRequest('/api/system/basic/job/many?ids=' + ids);
                        this.initJob();
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        });
                    });
            },
            //点击编辑，打开对话框
            showEditView(index,data){
                this.dialogVisible = true;
                Object.assign(this.updateJob,data);
            },
            //修改
            doUpdate(){
                this.putRequest('/api/system/basic/job/',this.updateJob).then(resp=>{
                    if(resp){
                        this.dialogVisible = false;
                        this.initJob();
                    }
                })
            },
            //导出数据
            exportJob(){
                window.open('/api/system/basic/job/exportJob','_parent');
            },
            beforeUpload(){
                this.importDataBtnText='正在上传';
                this.importDataBtnIcon='el-icon-loading';
                this.importDataDisabled = true;
            },
            onSuccess(response, file, fileList){
                this.importDataBtnText='导入数据';
                this.importDataBtnIcon='el-icon-upload2';
                this.importDataDisabled = false;
                this.initEmp();

            },
            onError(err, file, fileList){
                this.importDataBtnText='导入数据';
                this.importDataBtnIcon='el-icon-upload2';
                this.importDataDisabled = false;

            },
        }
    }
</script>

<style scoped>

    .trStyle{
        margin-top: 10px;
    }
</style>